import { ajax, tools } from './ajax.js';

//#region 获取登录信息的部分

// 登录用户的相关信息
let tbUser = {};
let tbUserInfo = {};
let userOtherInfo = {};

let menus = document.querySelectorAll('#divMenu > div');
console.log('menus:', menus);

function queryUserInfo() {
  ajax.post('/user/auth/getUserInfo', {}, (data) => {
    console.log('登录用户的信息：', data);
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      menus[0].style.display = 'none';
      menus[1].style.display = 'block';
      showUserInfo();
    } else {
      showToast('获取登录信息失败，请先登录');
      menus[1].style.display = 'none';
      menus[0].style.display = 'block';
    }
  });
}

queryUserInfo();

//#endregion

//#region 显示信息的部分
let btnUser = document.getElementById('btnUser');
let ainfo = document.getElementById('ainfo');
let imgLogo = document.getElementById('imgLogo');

let sexinfo = {
  m: '男生',
  f: '女生',
  n: '保密',
};

function showUserInfo() {
  btnUser.innerHTML = tbUser.username;
  let sex = sexinfo[tbUserInfo.sex];
  ainfo.innerHTML = `${tbUser.nickname}(${sex})`;
  imgLogo.setAttribute('src', tbUserInfo.img);
}

//#endregion

//#region 安全退出
let alogout = document.getElementById('alogout');

alogout.addEventListener('click', () => {
  ajax.post('/user/auth/logout', {}, () => {
    location.href = 'index.html';
  });
});

//#endregion

//#region 修改密码
let ampwd = document.getElementById('ampwd');
let dialogPwd = document.getElementById('dialogPwd');
let txt_oldpwd = document.getElementById('txt_oldpwd');
let txt_password = document.getElementById('txt_password');
let btnPwd = document.getElementById('btnPwd');

let dpwd = new bootstrap.Modal(dialogPwd);

ampwd.addEventListener('click', () => {
  dpwd.show();
});

btnPwd.addEventListener('click', () => {
  let info = {
    oldpwd: tools.md5(txt_oldpwd.value),
    password: tools.md5(txt_password.value),
  };
  ajax.post('/user/auth/updatePwd', info, (data) => {
    showToast(data.message);
  });
});

//#endregion

//#region 修改密码信息
let amodify = document.getElementById('amodify');
let dialogInfo = document.getElementById('dialogInfo');
let txt_img = document.getElementById('txt_img');
let txt_info = document.getElementById('txt_info');
let txt_nickname = document.getElementById('txt_nickname');
let txt_qq = document.getElementById('txt_qq');
let txt_wechat = document.getElementById('txt_wechat');
let sexm = document.getElementById('sexm');
let sexf = document.getElementById('sexf');
let sexn = document.getElementById('sexn');
let btnInfo = document.getElementById('btnInfo');

let dinfo = new bootstrap.Modal(dialogInfo);

amodify.addEventListener('click', () => {
  txt_nickname.value = tbUser.nickname;
  txt_img.value = tbUserInfo.img;
  txt_info.value = tbUserInfo.info;
  txt_qq.value = tbUserInfo.qq;
  txt_wechat.value = tbUserInfo.wechat;
  if (tbUserInfo.sex == 'm') {
    sexm.checked = true;
  } else if (tbUserInfo.sex == 'f') {
    sexf.checked = true;
  } else {
    sexn.checked = true;
  }

  dinfo.show();
});

btnInfo.addEventListener('click', () => {
  let info = {
    img: txt_img.value,
    info: txt_info.value,
    qq: txt_qq.value,
    wechat: txt_wechat.value,
    nickname: txt_nickname.value,
  };
  if (sexm.checked) {
    info.sex = 'm';
  } else if (sexf.checked) {
    info.sex = 'f';
  } else {
    info.sex = 'n';
  }
  ajax.post('/user/auth/updateUserInfo', info, (data) => {
    showToast(data.message);
    // 修改成功要重新查询一下用户的信息
    if (data.success) {
      queryUserInfo();
    }
  });
});

//#endregion

//#region 绑定邮箱
let aemail = document.getElementById('aemail');
let dialogEmail = document.getElementById('dialogEmail');
let txt_email = document.getElementById('txt_email');
let txt_emailcode = document.getElementById('txt_emailcode');
let btn_sendemail = document.getElementById('btn_sendemail');
let btnEmail = document.getElementById('btnEmail');

let edialog = new bootstrap.Modal(dialogEmail);

aemail.addEventListener('click', () => {
  if (tbUserInfo.email) {
    txt_email.value = tbUserInfo.email;
  }
  edialog.show();
});

btn_sendemail.addEventListener('click', () => {
  ajax.post(
    '/tool/sendEmailCode',
    {
      email: txt_email.value,
    },
    (data) => {
      showToast(data.message);
    }
  );
});

btnEmail.addEventListener('click', () => {
  ajax.post(
    '/user/auth/updateUserEmail',
    {
      email: txt_email.value,
      code: txt_emailcode.value,
    },
    (data) => {
      showToast(data.message);
      // 修改成功要重新查询一下用户的信息
      if (data.success) {
        queryUserInfo();
      }
    }
  );
});

//#endregion

//#region 信息弹出层的部分
let divToast = document.getElementById('divToast');
// querySelector是通过css选择器获取页面元素（只会返回一个）
let toastBody = document.querySelector('#divToast .toast-body');
// 创建toast对象
const toast = new bootstrap.Toast(divToast);

// 显示toast对话框
function showToast(message) {
  // 修改内容
  toastBody.innerHTML = message;
  // 显示
  toast.show();
}
//#endregion
